package org.kernely.stream.administration;

import org.kernely.ui.mvp.IKernelyView;

import com.vaadin.data.Container;
import com.vaadin.data.Property;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.terminal.Sizeable;
import com.vaadin.ui.AbstractSelect;
import com.vaadin.ui.Button;
import com.vaadin.ui.ComboBox;
import com.vaadin.ui.Component;
import com.vaadin.ui.DefaultFieldFactory;
import com.vaadin.ui.Field;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.Table;
import com.vaadin.ui.VerticalLayout;

public class StreamUserRightsView extends VerticalLayout implements IKernelyView{

	/**
	 * Serial Version ID
	 */
	private static final long serialVersionUID = -8836087119950168023L;

	public final static int SUBSCRIPTION_RIGHT = 0;
	public final static int REDACTION_RIGHT = 1;
	public final static int DELETE_RIGHT = 2;


	private HorizontalLayout addLayout;
	private ComboBox usersBox; // Where you can choose a user
	private Table table;
	private Label usersLabel;
	private Button addButton;
	private Button deleteButton;

	private final class ImmediateFieldFactory extends DefaultFieldFactory {
		/**
		 * Serial Version ID
		 */
		private static final long serialVersionUID = -8250868461921657L;

		public Field createField(Container container, Object itemId, Object propertyId,  Component uiContext) {
			if(!propertyId.equals("Rights")){
				return null;
			}
			else{
				ComboBox cb = new ComboBox();
				cb.addItem(SUBSCRIPTION_RIGHT);
				cb.setItemCaption(SUBSCRIPTION_RIGHT, "Subscription");
				cb.addItem(REDACTION_RIGHT);
				cb.setItemCaption(REDACTION_RIGHT, "Subscription/Redaction");
				cb.addItem(DELETE_RIGHT);
				cb.setItemCaption(DELETE_RIGHT, "Subscription/Redaction/Delete messages");
				cb.setImmediate(true);
				cb.setNullSelectionAllowed(false);
				cb.setValue(SUBSCRIPTION_RIGHT);
				return cb;
			}
		}
	}


	public StreamUserRightsView(){
		this.usersLabel = new Label("Users");

		// The combo box containing selectable users
		this.usersBox = new ComboBox();
		this.usersBox.setImmediate(true);
		this.usersBox.setNullSelectionAllowed(false);
		this.usersBox.setInputPrompt("Please select a user");
		this.usersBox.setWidth(225, Sizeable.UNITS_PIXELS);
		this.usersBox.setFilteringMode(AbstractSelect.Filtering.FILTERINGMODE_CONTAINS);

		this.table = new Table();
		this.table.addContainerProperty("Name", String.class, null, "Name", null, null);
		this.table.addContainerProperty("Rights", Integer.class, null, "Rights", null, null);

		this.table.setSelectable(true);
		this.table.setImmediate(true);
		this.table.setEditable(true);
		this.table.setSizeFull();

		this.table.setSortContainerPropertyId("Name");
		this.table.setSortAscending(true);
		this.table.setTableFieldFactory(new ImmediateFieldFactory());
		this.table.setPageLength(4);
		this.table.setHeight(155, Sizeable.UNITS_PIXELS);
		this.table.setMultiSelect(true);

		this.addButton =  new Button("Add");
		this.addButton.setWidth(80,UNITS_PIXELS);
		this.deleteButton = new Button("Delete");
		this.addButton.setEnabled(false);
		this.deleteButton.setEnabled(false);
		this.addLayout = new HorizontalLayout();
		this.addLayout.addComponent(usersBox);
		this.addLayout.addComponent(addButton);

		this.addComponent(this.usersLabel);
		this.addComponent(this.addLayout);
		this.addComponent(this.table);
		this.addComponent(this.deleteButton);

		// Basic behaviour of the buttons
		this.table.addListener(new Property.ValueChangeListener() {
			private static final long serialVersionUID = -5368063708649993389L;

			/**
			 * The method called when a user select an item in the list.
			 */
			public void valueChange(ValueChangeEvent event) {
				if (table.getValue() == null){
					deleteButton.setEnabled(false);
				}
				else {
					deleteButton.setEnabled(true);
				}
			}
		});
		this.usersBox.addListener(new Property.ValueChangeListener() {
			private static final long serialVersionUID = -5368063708649993389L;

			/**
			 * The method called when a user select an item in the list.
			 */
			public void valueChange(ValueChangeEvent event) {
				if (usersBox.getValue() == null){
					addButton.setEnabled(false);
				}
				else {
					addButton.setEnabled(true);
				}
			}
		});

		this.addLayout.setSpacing(true);
		this.setWidth(400, Sizeable.UNITS_PIXELS);
		this.setMargin(true,true,true,true);
		this.setSpacing(true);
	}

	public final Component getComponent() {
		return this;
	}

	/**
	 * @return the usersBox
	 */
	public final ComboBox getUsersBox() {
		return usersBox;
	}

	/**
	 * @return the table
	 */
	public final Table getTable() {
		return table;
	}

	/**
	 * @return the addButton
	 */
	public final Button getAddButton() {
		return addButton;
	}

	/**
	 * @return the deleteButton
	 */
	public final Button getDeleteButton() {
		return deleteButton;
	}

}
